package com.tmt.im.wallet.controler;

import com.alibaba.fastjson2.JSONObject;
import com.tmt.im.common.enums.RedPackageType;
import com.tmt.im.common.pojo.RedPackageDetailPoJo;
import com.tmt.im.wallet.controler.vo.*;
import com.tmt.im.wallet.helper.PrincipalHelper;
import com.tmt.im.wallet.service.RedPackageService;
import com.tmt.springboot.common.restful.ExecuteResult;
import com.tmt.springboot.common.restful.IExecuteResult;
import com.tmt.springboot.common.restful.ResultWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Page;
import org.springframework.scheduling.annotation.Async;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @Description TODO
 * @Author wuyi
 * @Date 2024/9/2 14:39
 * @Version 1.0
 **/
@Async("asyncTaskExecutor")
@Tag(name = "红包收发相关接口", description = "红包收发相关接口")
@RestController
@RequestMapping("${api-url-prefix}/redPackage")
public class RedPackageController {

    private final RedPackageService redPackageService;

    public RedPackageController(RedPackageService redPackageService) {
        this.redPackageService = redPackageService;
    }

    @Operation(summary = "发送红包",
            description = "发送红包",
            operationId = "createRedPackage",
            parameters = {
                    @Parameter(name = "request", description = "新红包创建请求对象")
            })
    @PostMapping(value = "/create")
    public CompletableFuture<IExecuteResult<ResultWrapper<Long>>> createRedPackage(@RequestBody @Validated NewRedPackageRequest request, Principal principal) {
        if (request.getType() == RedPackageType.LUCKY) {
            if (request.getSplitCount() == null || request.getSplitCount() <= 0) {
                return CompletableFuture.completedFuture(ExecuteResult.failure("发送手气红包时splitCount参数必须设置", null));
            }
            request.setReceiverIds(null);
        } else {
            if (request.getSplitCount() == null || request.getSplitCount() <= 0) {
                request.setSplitCount(1);
            }
            if (request.getReceiverIds().size() > 1) {
                return CompletableFuture.completedFuture(ExecuteResult.failure("receiverIds参数异常：发送非手气红包时领取人不能是多人", null));
            }
        }
        Long currentUserId = PrincipalHelper.getCurrentUserId(principal);
        IExecuteResult<Long> result = redPackageService.createRedPackage(currentUserId, request, principal);
        return CompletableFuture.completedFuture(ResultWrapper.from(result));
    }

    @Operation(summary = "领取红包接口",
            description = "领取红包接口",
            operationId = "receiveRedPackage",
            parameters = {
                    @Parameter(name = "param", description = "要领取的红包ID参数的JSONObject包装对象")
            })
    @PostMapping(value = "/receive")
    public CompletableFuture<IExecuteResult<RedPackageDetailInfo>> receiveRedPackage(@RequestBody JSONObject param, Principal principal) {
        Long redPackageId = null;
        try {
            redPackageId = param.getLong("redPackageId");
        } catch (Exception ex) {
            return CompletableFuture.completedFuture(ExecuteResult.failure("redPackageId参数读取失败", null));
        }
        Long currentUserId = PrincipalHelper.getCurrentUserId(principal);
        IExecuteResult<RedPackageDetailInfo> result = redPackageService.receiveRedPackage(currentUserId, redPackageId, principal);
        return CompletableFuture.completedFuture(result);
    }

    @Operation(summary = "查询红包领取完毕状态",
            description = "查询红包领取完毕状态",
            operationId = "queryRedPackageStatus",
            parameters = {
                    @Parameter(name = "redPackageId", description = "要查询状态的红包ID")
            })
    @GetMapping(value = "/status")
    public CompletableFuture<IExecuteResult<RedPackageStatus>> queryRedPackageStatus(@RequestParam("redPackageId") Long redPackageId,
                                                                                     Principal principal) {
        Long currentUserId = PrincipalHelper.getCurrentUserId(principal);
        IExecuteResult<RedPackageStatus> result = redPackageService.queryRedPackageStatus(currentUserId, redPackageId, principal);
        return CompletableFuture.completedFuture(result);
    }

    @Operation(summary = "红包领取详情",
            description = "查询指定红包的领取详情",
            operationId = "receiveRedPackageDetail",
            parameters = {
                    @Parameter(name = "redPackageId", description = "目标红包的ID")
            })
    @PostMapping(value = "/detail")
    public CompletableFuture<IExecuteResult<RedPackageDetailInfo>> receiveRedPackageDetail(@RequestParam("redPackageId") Long redPackageId, Principal principal) {
        IExecuteResult<RedPackageDetailInfo> result = redPackageService.receiveRedPackageDetail(redPackageId);
        return CompletableFuture.completedFuture(result);
    }

    @Operation(summary = "分页查询领取过的红包列表",
            description = "分页查询领取过的红包列表",
            operationId = "getReceivedRedPackagePage",
            parameters = {
                    @Parameter(name = "request", description = "领取的红包列表分页查询请求对象")
            })
    @PostMapping(value = "/page/receivedList")
    public CompletableFuture<IExecuteResult<ReceivedRedPackageResponse>> getReceivedRedPackagePage(@RequestBody RedPackagePageQueryRequest request,
                                                                                                     Principal principal) {
        Long currentUserId = PrincipalHelper.getCurrentUserId(principal);
        IExecuteResult<ReceivedRedPackageResponse> result = redPackageService.getReceivedRedPackagePage(currentUserId, request);
        return CompletableFuture.completedFuture(result);
    }

    @Operation(summary = "分页查询发出的红包列表",
            description = "分页查询发出的红包列表",
            operationId = "getSendRedPackagePage",
            parameters = {
                    @Parameter(name = "request", description = "发出的红包列表分页查询请求对象")
            })
    @PostMapping(value = "/page/createdList")
    public CompletableFuture<IExecuteResult<SendRedPackageResponse>> getSendRedPackagePage(@RequestBody RedPackagePageQueryRequest request,
                                                                                           Principal principal) {
        Long currentUserId = PrincipalHelper.getCurrentUserId(principal);
        IExecuteResult<SendRedPackageResponse> result = redPackageService.getSendRedPackagePage(currentUserId, request);
        return CompletableFuture.completedFuture(result);
    }

}
